package com.xzakota.minesweeper.controller;

import com.xzakota.minesweeper.R;

import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 游戏资源管理器
 *
 * @author Xzakota
 */
public class GameResource {
    public static final String THEME_DEFAULT = "default";
    private static final R DEFAULT_RESOURCE = new R(THEME_DEFAULT);
    public static R CURRENT_RESOURCE = DEFAULT_RESOURCE;
    private final static Map<String, R> RESOURCE_MAP = new HashMap<>();

    static {
        RESOURCE_MAP.put(THEME_DEFAULT, DEFAULT_RESOURCE);
    }

    public static void setTheme(String theme) {
        CURRENT_RESOURCE = Objects.requireNonNullElse(RESOURCE_MAP.get(theme), DEFAULT_RESOURCE);
    }

    public static ImageIcon getIconResource(int id) {
        ImageIcon r = CURRENT_RESOURCE.ICON_LIST.get(id);
        if (r == null && CURRENT_RESOURCE != DEFAULT_RESOURCE) {
            return DEFAULT_RESOURCE.getIconResource(id);
        }
        return r;
    }

    public static BufferedImage getImageResource(int id) {
        BufferedImage r = CURRENT_RESOURCE.IMAGE_LIST.get(id);
        if (r == null && CURRENT_RESOURCE != DEFAULT_RESOURCE) {
            return DEFAULT_RESOURCE.getImageResource(id);
        }
        return r;
    }

    public static Clip getSoundResource(int id) {
        Clip r = CURRENT_RESOURCE.SOUND_LIST.get(id);
        if (r == null && CURRENT_RESOURCE != DEFAULT_RESOURCE) {
            return DEFAULT_RESOURCE.getSoundResource(id);
        }
        return r;
    }

    public static URL getHTMLResource(int id) {
        URL r = CURRENT_RESOURCE.HTML_LIST.get(id);
        if (r == null && CURRENT_RESOURCE != DEFAULT_RESOURCE) {
            return DEFAULT_RESOURCE.getHTMLResource(id);
        }
        return r;
    }
}
